<?php

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

/**
 * View class for the Settings screen
 */
class TaxibookingViewSettings extends JView {

	function display($tpl = null) {

//		global $mainframe;
$mainframe		= JFactory::getApplication();
		//initialise variables
		$document 	=  JFactory::getDocument();
		$acl		=  JFactory::getACL();
		$uri 		= JFactory::getURI();
		$user 		= JFactory::getUser();
                $editor 	= JFactory::getEditor();
               
		//get data from model
		$model		= & $this->getModel();
		$elsettings = & $this->get( 'Data');

                $groups = $user->get('groups');
              $groups = array_values($groups);

		//only admins and super users have access to this view
		if (!in_array($groups[0],array('8','7','6'))) {
			JError::raiseWarning( 'SOME_ERROR_CODE', JText::_( 'ALERTNOTAUTH'));
			$mainframe->redirect( 'index.php?option=com_taxibooking&view=orders' );
		}

		// fail if checked out not by 'me'
//		if ($model->isCheckedOut( $user->get('id') )) {
//			JError::raiseWarning( 'SOME_ERROR_CODE', JText::_( 'EDITED BY ANOTHER ADMIN' ));
//			$mainframe->redirect( 'index.php?option=com_taxibooking&view=orders' );
//		}
		
		//add css, js and submenu to document
		$document->addStyleSheet('components/com_taxibooking/assets/css/bookingbackend.css');

		//create the toolbar
		JToolBarHelper::title( JText::_( 'SETTINGS' ), 'generic' );
		//Create Submenu
		JSubMenuHelper::addEntry( JText::_( 'HOME' ), 'index.php?option=com_taxibooking');
		JSubMenuHelper::addEntry( JText::_( 'ITEMS' ), 'index.php?option=com_taxibooking&view=orders');
		JSubMenuHelper::addEntry( JText::_( 'INTEREST_POINTS' ), 'index.php?option=com_taxibooking&view=points');
                JSubMenuHelper::addEntry(JText::_( 'ROUTES' ), 'index.php?option=com_taxibooking&view=routes');
		JSubMenuHelper::addEntry( JText::_( 'COUNTYS' ), 'index.php?option=com_taxibooking&view=countys');
		JSubMenuHelper::addEntry( JText::_( 'DISTRICTS' ), 'index.php?option=com_taxibooking&view=districts');
                JSubMenuHelper::addEntry( JText::_( 'SETTINGS' ), 'index.php?option=com_taxibooking&view=settings', true);
		
				
		JToolBarHelper::apply();
		JToolBarHelper::spacer();
		JToolBarHelper::save('save');
		JToolBarHelper::spacer();
		JToolBarHelper::cancel();

                //Get the lists
		$lists = $this->_buildEditLists($elsettings);

                  //build image select js and load the view
		$js = "
		function elSelectImage(image, imagename) {
			document.getElementById('a_image').value = image;
			document.getElementById('a_imagename').value = imagename;
			document.getElementById('imagelib').src = '../images/stories/taxibooking/' + image;
			document.getElementById('sbox-window').style.display='none';
			document.getElementById('sbox-overlay').style.display='none';
		}";

		$link = 'index.php?option=com_taxibooking&amp;view=imagehandler&amp;layout=uploadimage&amp;task=headerlogo&amp;tmpl=component';
		$link2 = 'index.php?option=com_taxibooking&amp;view=imagehandler&amp;task=selectheaderlogo&amp;tmpl=component';
		$document->addScriptDeclaration($js);
		$imageselect = "\n<input style=\"background: #ffffff;\" type=\"text\" id=\"a_imagename\" value=\"$elsettings->header_logo\" disabled=\"disabled\" onchange=\"javascript:if (document.forms[0].a_imagename.value!='') {document.imagelib.src='../images/stories/taxibooking/' + document.forms[0].a_imagename.value} else {document.imagelib.src='../images/blank.png'}\"; />";

		$imageselect .= "<div class=\"button2-left\"><div class=\"blank\"><a class=\"modal\" title=\"".JText::_('Upload')."\" href=\"$link\" rel=\"{handler: 'iframe', size: {x: 650, y: 375}}\">".JText::_('Upload')."</a></div></div>\n";
		$imageselect .= "<div class=\"button2-left\"><div class=\"blank\"><a class=\"modal\" title=\"".JText::_('SELECTIMAGE')."\" href=\"$link2\" rel=\"{handler: 'iframe', size: {x: 650, y: 375}}\">".JText::_('SELECTIMAGE')."</a></div></div>\n";

		$imageselect .= "\n&nbsp;<input class=\"inputbox\" type=\"button\" onclick=\"elSelectImage('', '".JText::_('SELECTIMAGE')."' );\" value=\"".JText::_('Reset')."\" />";
		$imageselect .= "\n<input type=\"hidden\" id=\"a_image\" name=\"header_logo\" value=\"$elsettings->header_logo\" />";

		//assign data to template
                $this->assignRef('imageselect'	, $imageselect);
		$this->assignRef('elsettings'	, $elsettings);
                $this->assignRef('lists'	, $lists);
		$this->assignRef('request_url'	, $uri->toString());
                $this->assignRef('editor'		, $editor);

		parent::display($tpl);

	}
        
        /**
	 * Creates the item submit form
	 *
	 * @since 1.0
	 */
	function _buildEditLists($elsettings)
	{
                $db = & JFactory::getDBO();
               
                $query = 'SELECT `currency_id`, `currency_code`, `currency_name`'
                        . ' FROM #__taxibooking_currency'
                        . ' ORDER BY `currency_name` ASC'
                        ;
                $db->setQuery($query);
                $db->query();
                $rows = $db->loadObjectList();
                
		$currency_options = array();
                $currency_options[] = JHTML::_('select.option',  '', JText::_( 'SELECT CURRENCY' ) );
                if(!empty($rows)){
                    foreach ($rows as $row){
                        $currency_options[] = JHTML::_('select.option',  $row->currency_code, $row->currency_name );
                    }
                }
                $lists['currency'] = JHTML::_('select.genericlist', $currency_options, 'currency', '', 'value', 'text', $elsettings->currency );
		
		return $lists;
	}
}